package com.anywhere.freightman.common.ex;

import com.anywhere.freightman.common.entity.ReturnResp;
import com.anywhere.freightman.common.enumeration.StatusCode;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
@ResponseBody
public class CustomExceptionHandler {

    @ExceptionHandler(CustomException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ReturnResp handleException(Exception e) {
        return new ReturnResp(StatusCode.FAIL, e.getMessage(), e.getLocalizedMessage());
    }

    @ExceptionHandler(ForbiddenException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public ReturnResp forbiddenException(Exception e) {
        return new ReturnResp(StatusCode.FAIL, e.getMessage(), null);
    }

    @ExceptionHandler(RuntimeException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ReturnResp handleRuntimeException(Exception e) {
        e.printStackTrace();
        return new ReturnResp(StatusCode.FAIL, "系统错误", e);
    }
}
